package com.reger.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.reger.core.Context;
import com.reger.core.init.LoginInitalize;
import com.reger.core.init.TokenOutService;
import com.reger.core.model.ResponseEntity;
import com.reger.core.model.SysUser;

@Component
public class ContextUtils {
	private static final Logger logger = LoggerFactory.getLogger(ContextUtils.class);
	private static LoginInitalize loginInitalize;

	private static TokenOutService tokenOutService;

	@Autowired(required = false)
	void setLoginInitalize(LoginInitalize loginInitalize) {
		ContextUtils.loginInitalize = loginInitalize;
	}

	@Autowired(required = false)
	public void setTokenOutService(TokenOutService tokenOutService) {
		ContextUtils.tokenOutService = tokenOutService;
	}

	public static SysUser initLoginInfo() {
		if (loginInitalize == null) {
			logger.debug("不需要对输出的token加密");
			return null;
		}
		SysUser user = loginInitalize.findLoginUser();
		Context.setUser(user);
		return user;
	}

	public static void setToken(ResponseEntity<?> responseEntity) {
		if (tokenOutService == null) {
			logger.debug("不需要对输出的token加密");
			return;
		}
		tokenOutService.setToken(responseEntity);
	}
}
